package me.keiwu.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.Token;
import me.keiwu.pub.data.wx.WXStepData;
import me.keiwu.pub.util.AreaUtil;
import me.keiwu.pub.util.DateUtil;
import me.keiwu.pub.util.TokenUtil;
import me.keiwu.user.dao.AreaDao;
import me.keiwu.user.dao.UserLbsDao;
import me.keiwu.user.domain.Area;
import me.keiwu.user.domain.User;
import me.keiwu.user.domain.UserLbs;
import me.keiwu.user.domain.UserStep;
import me.keiwu.user.domain.UserTotalStep;
import me.keiwu.user.service.RobotService;
import me.keiwu.user.service.StepService;
import me.keiwu.user.service.UserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by kei on 11/01/2018.
 */
@Service
@Transactional
public class RobotServiceImpl implements RobotService {


    @Autowired
    private UserService userService;
    @Autowired
    private TokenUtil tokenUtil;
    @Autowired
    private StepService stepService;
    @Autowired
    private AreaDao areaDao;
    @Autowired
    private UserLbsDao userLbsDao;

    @Override
    public ResponseV2 login() {
        String openId = RandomStringUtils.randomAlphanumeric(32);
        String name = RandomStringUtils.randomAlphabetic(6);
        User user = new User();
        user.setGold(10000);
        user.setChannel("robot");
        user.setNickName("robot-" + name);
        user.setOpenId(openId);
        user.setUnionId(openId);
        user.setWxAvatarUrl("https://gss2.bdstatic.com/-fo3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268/sign=80a3f66e3e6d55fbc5c6712055234f40/023b5bb5c9ea15ce90e77ab9b5003af33a87b271.jpg");
        userService.save(user);
        // 刷新或者生成token
        String tokenStr = tokenUtil.getAccessToken(user.getId());
        Token token;
        if (StringUtils.isBlank(tokenStr)) {
            token = tokenUtil.createAccessToken(user.getId());
        } else {
            token = tokenUtil.updateAccessToken(user.getId(), tokenStr);
        }
        JSONObject ret = new JSONObject();
        ret.put("userId", user.getId());
        ret.put("openId", user.getOpenId());
        ret.put("accessToken", token.getToken());
        return ResponseV2.ok(ret);
    }


    @Override
    public ResponseV2 commitStep(long userId, WXStepData stepData) {
        List<UserStep> userSteps = stepService.saveStepData(userId, stepData);
        int days = DateUtil.getDayStrInt(new Date());
        UserTotalStep totalStep = userService.getUserTotalStep(userId);
        UserStep dailyStep = null;
        for (UserStep step : userSteps) {
            if (days == step.getDays()) {
                dailyStep = step;
                break;
            }
        }
        if (dailyStep == null) {
            return ResponseV2.fail("获取不了步数");
        }
        JSONObject json = new JSONObject();
        json.put("dailyStep", dailyStep);
        json.put("totalStep", totalStep.getTotalStep() + dailyStep.getStep());
        json.put("totalStep30", totalStep.getTotalStep30());
        return ResponseV2.ok(json);
    }


    @Override
    public ResponseV2 commitLbs(long userId) {
        Area area = areaDao.findRandByType(3);
        UserLbs ul = new UserLbs();
        ul.setUserId(userId);
        ul.setLatitude(0.0);
        ul.setLongitude(0.0);
        long cityCode = area.getCode();
        // 区
        ul.setDistrict(AreaUtil.getDistrict(cityCode));
        // 市
        ul.setCity(AreaUtil.getCity(cityCode));
        // 省
        ul.setProvince(AreaUtil.getProvince(cityCode));
        ul.setUpdateTime(DateUtil.getUnixTime());
        userLbsDao.save(ul);
        return ResponseV2.ok();
    }


}
